//
//  DISBMCenterViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/5.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISBMCenterViewController.h"
#import "CSMyInfoViewController.h"
#import "DISBcomtitlViewController.h"
#import "CSPersonModel.h"
#import "CSaddPerCardViewController.h"
#import "CSIdAuthViewController.h"
#import "BaccListViewController.h"
#import "BinvitiveViewController.h"
#import "MyacontViewController.h"
#import "BwebviewViewController.h"
#import "CSIdAuthViewController.h"

#import "ZTimeOutOrderVC.h"//超时未接单

@interface DISBMCenterViewController ()
@property (nonatomic,strong) CSPersonModel *model;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *ivHeader;//头像
@property (nonatomic, strong) UILabel *lblNickname;//昵称
@property (nonatomic, strong) UILabel *lblAgent;//代理
@property (nonatomic, strong) UILabel *lblAddress;//地址
@property (nonatomic, strong) UILabel *lblComplainLeft;//未处理投诉
@property (nonatomic, strong) UILabel *lblComplainRight;//处理中投诉
@end

@implementation DISBMCenterViewController

- (instancetype)init{
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tuisonhg:) name:APPDELEGATE_REQUEST_REFRESHLISTDATA object:nil];
    }
    return self;
}
-(void)tuisonhg:(UIButton *)but{
    [self requestcount];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    //状态栏字体白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    if (self.model) {
        [self request];
    }
    [self requestcount];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    //状态栏字体黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.model = [[CSPersonModel alloc]init];
    [self setupNavViewAction];
    [self setupViewAction];
    
    [self request];
}
#pragma mark - 请求个人信息
-(void)request{
    
    kWeakSelf;
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        weakSelf.model = [CSPersonModel mj_objectWithKeyValues:[dic objectForKey:@"data"]];
        [weakSelf handleDataAction];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
#pragma mark - 请求投诉信息
-(void)requestcount{
    [NetWorkTool POST:complaintCount param:nil success:^(id dic) {
        
        self.lblComplainLeft.text = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"data"] objectForKey:@"countNo"]];
        self.lblComplainRight.text = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"data"] objectForKey:@"countYes"]];
        
        if ([[[dic objectForKey:@"data"] objectForKey:@"is_power"]  integerValue] == 0 ) {
            
        }
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
#pragma mark - 自定义导航栏
- (void)setupNavViewAction{
    UIView *viewNav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kNaviHeight)];
    [self.view addSubview:viewNav];
    
    UIImageView *ivBg = [[UIImageView alloc] initWithFrame:viewNav.bounds];
    ivBg.image = imgname(@"base_nav_bg");
    [viewNav addSubview:ivBg];
    
    UILabel *lblNav = [[UILabel alloc] initWithFrame:CGRectMake(0, kNaviHeight - 44, kScreen_Width, 44)];
    lblNav.text = @"我的";
    lblNav.font = [UIFont systemFontOfSize:18];
    lblNav.textColor = kColorWithHex(0xffffff);
    lblNav.textAlignment = NSTextAlignmentCenter;
    [viewNav addSubview:lblNav];
}
#pragma mark - 界面布局方法
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight)];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 542)];
    viewHeader.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.tableHeaderView = viewHeader;
    
    UIView *viewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 146)];
    viewTop.backgroundColor = kColorWithHex(0xffffff);
    [viewHeader addSubview:viewTop];
    
    _ivHeader = [[UIImageView alloc] initWithFrame:CGRectMake(16, 16, 40, 40)];
    _ivHeader.layer.cornerRadius = 20;
    _ivHeader.layer.masksToBounds = YES;
    _ivHeader.image = imgname(@"headerIMage");
    [viewTop addSubview:_ivHeader];
    
    _lblNickname = [UILabel new];
    _lblNickname.font = [UIFont systemFontOfSize:16];
    _lblNickname.text = @"昵称";
    _lblNickname.textColor = kColorWithHex(0x666666);
    _lblNickname.textAlignment = NSTextAlignmentLeft;
    _lblNickname.preferredMaxLayoutWidth = 110;
    [viewTop addSubview:_lblNickname];
    [_lblNickname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.ivHeader.mas_trailing).offset(3);
        make.top.equalTo(self.ivHeader.mas_top);
        make.height.equalTo(@(22));
    }];
    
    _lblAgent = [UILabel new];
    _lblAgent.font = [UIFont systemFontOfSize:12];
    _lblAgent.text = @"我是市县级经理";
    _lblAgent.textColor = kColorWithHex(0xfaf6dd);
    _lblAgent.textAlignment = NSTextAlignmentCenter;
    _lblAgent.backgroundColor = kColorWithHex(0xed9300);
    _lblAgent.layer.cornerRadius = 10;
    _lblAgent.layer.masksToBounds = YES;
    _lblAgent.hidden = YES;
    [viewTop addSubview:_lblAgent];
    [_lblAgent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lblNickname.mas_centerY);
        make.leading.equalTo(self.lblNickname.mas_trailing).offset(20);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    _lblAddress = [UILabel new];
    _lblAddress.text = @"地址";
    _lblAddress.font = [UIFont systemFontOfSize:13];
    _lblAddress.textAlignment = NSTextAlignmentLeft;
    _lblAddress.textColor = kColorWithHex(0x666666);
    [viewTop addSubview:_lblAddress];
    [_lblAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.ivHeader.mas_bottom);
        make.leading.equalTo(self.lblNickname);
        make.height.equalTo(@(18));
    }];
    
    UIImageView *ivArrow = [UIImageView new];
    ivArrow.image = imgname(@"Jinru");
    [viewTop addSubview:ivArrow];
    [ivArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.ivHeader.mas_centerY);
        make.trailing.equalTo(viewTop.mas_trailing);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    UIButton *btnUser = [UIButton buttonWithType:UIButtonTypeCustom];
    btnUser.frame = CGRectMake(16, 16, kScreen_Width - 16, 40);
    [viewTop addSubview:btnUser];
    [btnUser addTarget:self action:@selector(btnUserClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *lblNoticeLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, 98, kScreen_Width / 2.0, 20)];
    lblNoticeLeft.text = @"未处理投诉";
    lblNoticeLeft.font = [UIFont systemFontOfSize:14];
    lblNoticeLeft.textColor = kColorWithHex(0x70be68);
    lblNoticeLeft.textAlignment = NSTextAlignmentCenter;
    [viewTop addSubview:lblNoticeLeft];
    _lblComplainLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, 118, kScreen_Width / 2.0, 22)];
    _lblComplainLeft.text = @"0";
    _lblComplainLeft.font = [UIFont systemFontOfSize:17];
    _lblComplainLeft.textColor = kColorWithHex(0x70be68);
    _lblComplainLeft.textAlignment = NSTextAlignmentCenter;
    [viewTop addSubview:_lblComplainLeft];
    
    UILabel *lblNoticeRight = [[UILabel alloc] initWithFrame:CGRectMake(lblNoticeLeft.width, 98, kScreen_Width / 2.0, 20)];
    lblNoticeRight.text = @"处理中投诉";
    lblNoticeRight.font = [UIFont systemFontOfSize:14];
    lblNoticeRight.textColor = kColorWithHex(0x70be68);
    lblNoticeRight.textAlignment = NSTextAlignmentCenter;
    [viewTop addSubview:lblNoticeRight];
    _lblComplainRight = [[UILabel alloc] initWithFrame:CGRectMake(_lblComplainLeft.width, 118, kScreen_Width / 2.0, 22)];
    _lblComplainRight.text = @"0";
    _lblComplainRight.font = [UIFont systemFontOfSize:17];
    _lblComplainRight.textColor = kColorWithHex(0x70be68);
    _lblComplainRight.textAlignment = NSTextAlignmentCenter;
    [viewTop addSubview:_lblComplainRight];
    
    UIButton *btnComplainLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    btnComplainLeft.frame = CGRectMake(0, 98, kScreen_Width / 2.0, 48);
    btnComplainLeft.tag = 200;
    [btnComplainLeft addTarget:self action:@selector(btnComplainClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:btnComplainLeft];
    
    UIButton *btnComplainRight = [UIButton buttonWithType:UIButtonTypeCustom];
    btnComplainRight.frame = CGRectMake(btnComplainLeft.width, 98, kScreen_Width / 2.0, 48);
    btnComplainRight.tag = 201;
    [btnComplainRight addTarget:self action:@selector(btnComplainClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:btnComplainRight];
    
    UIView *viewBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 158, kScreen_Width, 384)];
    viewBottom.backgroundColor = kColorWithHex(0xffffff);
    [viewHeader addSubview:viewBottom];
    
    UIView *viewLineV = [UIView new];
    viewLineV.backgroundColor = kColorWithHex(0xe5e5e5);
    [viewBottom addSubview:viewLineV];
    [viewLineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(viewBottom);
        make.top.equalTo(viewBottom.mas_top).offset(26);
        make.size.mas_equalTo(CGSizeMake(1, 343));
    }];
    
    UIView *viewLineHTop = [UIView new];
    viewLineHTop.backgroundColor = kColorWithHex(0xe5e5e5);
    [viewBottom addSubview:viewLineHTop];
    [viewLineHTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBottom.mas_top).offset(128);
        make.leading.equalTo(viewBottom.mas_leading).offset(20);
        make.trailing.equalTo(viewBottom.mas_trailing).offset(-20);
        make.height.mas_equalTo(1);
    }];
    
    UIView *viewLineHBottom = [UIView new];
    viewLineHBottom.backgroundColor = kColorWithHex(0xe5e5e5);
    [viewBottom addSubview:viewLineHBottom];
    [viewLineHBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBottom.mas_top).offset(256);
        make.leading.equalTo(viewBottom.mas_leading).offset(20);
        make.trailing.equalTo(viewBottom.mas_trailing).offset(-20);
        make.height.mas_equalTo(1);
    }];
    
    UIButton *btnComplainManager = [UIButton buttonWithType:UIButtonTypeCustom];
    btnComplainManager.frame = CGRectMake(kScreen_Width / 2.0 - 150, 22, 100, 86);
    [btnComplainManager setImage:imgname(@"tousuguanli") forState:UIControlStateNormal];
    [btnComplainManager setTitle:@"投诉管理" forState:UIControlStateNormal];
    [btnComplainManager setTitleColor:kColorWithHex(0x333333) forState:UIControlStateNormal];
    btnComplainManager.titleLabel.font = [UIFont systemFontOfSize:17];
    btnComplainManager.tag = 300;
    [btnComplainManager addTarget:self action:@selector(btnMenuClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBottom addSubview:btnComplainManager];
    [self btnChangeWith:btnComplainManager];
    
    UIButton *btnAgent = [UIButton buttonWithType:UIButtonTypeCustom];
    btnAgent.frame = CGRectMake(kScreen_Width / 2.0 + 50, 22, 100, 86);
    [btnAgent setImage:imgname(@"wodezhanghu") forState:UIControlStateNormal];
    [btnAgent setTitle:@"我的账户" forState:UIControlStateNormal];
    [btnAgent setTitleColor:kColorWithHex(0x333333) forState:UIControlStateNormal];
    btnAgent.titleLabel.font = [UIFont systemFontOfSize:17];
    btnAgent.tag = 301;
    [btnAgent addTarget:self action:@selector(btnMenuClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBottom addSubview:btnAgent];
    [self btnChangeWith:btnAgent];
    
    UIButton *btnInvite = [UIButton buttonWithType:UIButtonTypeCustom];
    btnInvite.frame = CGRectMake(kScreen_Width / 2.0 - 150, 128 + 22, 100, 86);
    [btnInvite setImage:imgname(@"yaoqingjiaru") forState:UIControlStateNormal];
    [btnInvite setTitle:@"邀请加入" forState:UIControlStateNormal];
    [btnInvite setTitleColor:kColorWithHex(0x333333) forState:UIControlStateNormal];
    btnInvite.titleLabel.font = [UIFont systemFontOfSize:17];
    btnInvite.tag = 302;
    [btnInvite addTarget:self action:@selector(btnMenuClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBottom addSubview:btnInvite];
    [self btnChangeWith:btnInvite];
    
    UIButton *btnRecord = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRecord.frame = CGRectMake(kScreen_Width / 2.0 + 50, 128 + 22, 100, 86);
    [btnRecord setImage:imgname(@"jiesunajilu") forState:UIControlStateNormal];
    [btnRecord setTitle:@"结算记录" forState:UIControlStateNormal];
    [btnRecord setTitleColor:kColorWithHex(0x333333) forState:UIControlStateNormal];
    btnRecord.titleLabel.font = [UIFont systemFontOfSize:17];
    btnRecord.tag = 303;
    [btnRecord addTarget:self action:@selector(btnMenuClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBottom addSubview:btnRecord];
    [self btnChangeWith:btnRecord];
    
    UIButton *btnTimeOut = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTimeOut.frame = CGRectMake(kScreen_Width / 2.0 - 150, 128 + 128 + 22, 100, 86);
    [btnTimeOut setImage:imgname(@"mine_menu_timeout_icon") forState:UIControlStateNormal];
    [btnTimeOut setTitle:@"超时未接单" forState:UIControlStateNormal];
    [btnTimeOut setTitleColor:kColorWithHex(0x333333) forState:UIControlStateNormal];
    btnTimeOut.titleLabel.font = [UIFont systemFontOfSize:17];
    btnTimeOut.tag = 304;
    [btnTimeOut addTarget:self action:@selector(btnMenuClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewBottom addSubview:btnTimeOut];
    [self btnChangeWith:btnTimeOut];
    
}
#pragma mark - 数据更新
- (void)handleDataAction{
    if ([self.model.image_url hasPrefix:@"http"]) {
        [self.ivHeader sd_setImageWithURL:[NSURL URLWithString:self.model.image_url] placeholderImage:[UIImage imageNamed:@"headerIMage"]];
    }else{
        sdimg(self.ivHeader,self.model.image_url);
    }
    self.lblNickname.text = self.model.name;
    self.lblAddress.text = self.model.area_name;
    self.lblAgent.hidden = NO;
    if (strIsEmpty(self.model.id_card)) {
        //没绑定身份证 去绑定
        CSIdAuthViewController *bild = [[CSIdAuthViewController alloc]init];
        bild.iscenter = @"center";
        [self.navigationController pushViewController:bild animated:YES];
    }
}
#pragma mark - 交互事件
//进入个人信息
- (void)btnUserClickAction:(UIButton *)sender{
    CSMyInfoViewController *vc = [CSMyInfoViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
//投诉处理
- (void)btnComplainClickAction:(UIButton *)sender{
    NSString *str = [USER_DEFAULT objectForKey:@"is_power"];
    if ([str isEqualToString:@"0"]) {
        ShowToastWithText(@"请到个人信息激活管理员身份");
        return;
    }
    NSInteger tag = sender.tag;
    if (tag == 200) {
        //未处理投诉
        DISBcomtitlViewController *vc = [[DISBcomtitlViewController alloc]init];
        vc.seleInd = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tag == 201){
        //处理中投诉
        DISBcomtitlViewController *vc = [[DISBcomtitlViewController alloc]init];
        vc.seleInd = 2;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
//底部菜单键
- (void)btnMenuClickAction:(UIButton *)sender{
    NSString *str = [USER_DEFAULT objectForKey:@"is_power"];
    if ([str isEqualToString:@"0"]) {
        ShowToastWithText(@"请到个人信息激活管理员身份");
        return;
    }
    NSInteger tag = sender.tag;
    if (tag == 300) {
        //投诉管理
        DISBcomtitlViewController *vc = [[DISBcomtitlViewController alloc]init];
        vc.seleInd = 0;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tag == 301){
        //我的账户
        if (_model) {
            MyacontViewController *vc = [[MyacontViewController alloc]init];
            vc.model = _model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else if (tag == 302){
        //邀请加入
        BwebviewViewController *vc = [BwebviewViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tag == 303){
        //结算记录
        BaccListViewController *vc = [BaccListViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tag == 304){
        //超时未接单
        ZTimeOutOrderVC *vc = [ZTimeOutOrderVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - 修改按钮的文字图片位置
- (void)btnChangeWith:(UIButton *)sender{
    float  spacing = 0;//图片和文字的上下间距
    CGSize imageSize = sender.imageView.frame.size;
    CGSize titleSize = sender.titleLabel.frame.size;
    CGSize textSize = [sender.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : sender.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    sender.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    sender.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
